package com.example.zhu95.mytimber.ui;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.ItemVdragAdapter;
import com.example.zhu95.mytimber.entity.SongBean;

import java.util.Collections;
import java.util.List;

/**
 * @author zhu95 on 2017/12/021.
 */
public class SongItemTouchCallBack extends ItemTouchHelper.Callback {
    private RecyclerView.Adapter adapter;
    protected List<SongBean> songs;

    public SongItemTouchCallBack(RecyclerView.Adapter adapter, List<SongBean> songs) {
        this.adapter = adapter;
        this.songs = songs;
    }

    // MARK:2017/12/20 设置拖拽方向 可判断布局管理器，LinearlayoutManager就是上下 GridLayoutManager就是上下左右拖拽
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
    }

    // MARK:2017/12/20 拖拽时不断调用改变item位置
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(songs, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(songs, i, i - 1);
            }
        }
        adapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    /**
     * MARK:2017/12/20 拖拽时的效果
     *
     * @param viewHolder
     * @param actionState
     */
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setAlpha(0.5f);
            if (viewHolder instanceof ItemVdragAdapter.ViewHolder) {
                ItemVdragAdapter.ViewHolder holder = (ItemVdragAdapter.ViewHolder) viewHolder;

                if (adapter instanceof ItemVdragAdapter) {
                    ItemVdragAdapter itemVdragAdapter = (ItemVdragAdapter) adapter;
                    if (itemVdragAdapter.getItemColor() == Color.WHITE) {
                        holder.imgDrag.setImageResource(R.drawable.drag_10point2_white);
                    }
                } else {
                    holder.imgDrag.setImageResource(R.drawable.drag_10point2_black);
                }
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * MARK:2017/12/20 松开的效果
     *
     * @param recyclerView
     * @param viewHolder
     */
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setAlpha(1f);
        if (viewHolder instanceof ItemVdragAdapter.ViewHolder) {
            ItemVdragAdapter.ViewHolder holder = (ItemVdragAdapter.ViewHolder) viewHolder;
            if (adapter instanceof ItemVdragAdapter) {
                ItemVdragAdapter itemVdragAdapter = (ItemVdragAdapter) adapter;
                if (itemVdragAdapter.getItemColor() == Color.WHITE) {
                    holder.imgDrag.setImageResource(R.drawable.drag_10point_white);
                }
            } else {
                holder.imgDrag.setImageResource(R.drawable.drag_10point_black);
            }
        }

        saveSequence();
    }

    /**
     * 推拽之后保存拖拽之后的顺序
     */
    public void saveSequence() {
    }
}
